home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3.iso / chapte19 / ex3.c < prev    next >
Text File  |  1995-05-14  |  5KB  |  123 lines

  1. #include <windows.h>
  2. #include <genstub.c>
  3. #include <winreg.h>
  4.  
  5. #define LISTBOX_ID             1000   // Identifies the child window.
  6.  
  7. EnumerateRegistry( HKEY hKey, HWND hWndListBox )
  8. {
  9.    DWORD      dwcSubKeys, dwcValues, dwcMaxSubKeyName, dwcMaxValueName, dwcMaxValueData;
  10.    char       szLevel[33];                  // Visually demonstrates the subkey level.
  11.    static int iLevel = 0;                   // Level counter
  12.    char       szBuffer[255];                // Format area for text output
  13.    DWORD      dwSubKeyIndex = 0;            // Counter for subkeys
  14.    DWORD      dwValueIndex = 0;             // Counter for values
  15.    HKEY       hNewKey = 0;                  // New key to iterate
  16.    LONG       lStatus = ERROR_SUCCESS;      // Status flag for enumeration
  17.  
  18.    // make level string "========="
  19.    iLevel++;                          // Next level now.
  20.    FillMemory( szLevel, iLevel, '=' );
  21.    szLevel[iLevel] = 0;
  22.  
  23.    //  Find out how much space we need for key names and values in this subtree.
  24.    RegQueryInfoKey( hKey, szBuffer, NULL, 0, &dwcSubKeys, &dwcMaxSubKeyName, NULL,
  25.                     &dwcValues, &dwcMaxValueName, &dwcMaxValueData, NULL, NULL );
  26.    if ( dwcValues != 0 || dwcSubKeys != 0 )
  27.    {
  28.        LPTSTR lpszSubKeyName = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
  29.                                           dwcMaxSubKeyName + 1 );
  30.        LPTSTR lpszValueName = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
  31.                                          dwcMaxValueName + 1 );
  32.        LPTSTR lpszValueData = HeapAlloc( GetProcessHeap( ), HEAP_ZERO_MEMORY,
  33.                                          dwcMaxValueData + 1 );
  34.        // Enumerate values before subkeys.
  35.        do
  36.        {
  37.            DWORD dwType;
  38.            DWORD dwcValueName = dwcMaxValueName + 1;
  39.            DWORD dwcValueData = dwcMaxValueData + 1;
  40.  
  41.            lStatus = RegEnumValue( hKey, dwValueIndex,lpszValueName,&dwcValueName,
  42.                                    NULL, &dwType, lpszValueData, &dwcValueData );
  43.            if ( lStatus == ERROR_SUCCESS )
  44.            {
  45.                 wsprintf( szBuffer, "%s> VALUE [%s]=[%s], Type=%s", szLevel,
  46.                           lpszValueName, (dwType==REG_SZ) ? lpszValueData : "?",
  47.                           (dwType==REG_SZ) ? "REG_SZ" : "OTHER" );
  48.                 SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM) szBuffer );
  49.            }
  50.            dwValueIndex++;
  51.        } while ( lStatus==ERROR_SUCCESS );
  52.  
  53.        // Enumerate subkeys now.
  54.        do
  55.        {
  56.            DWORD dwcSubKeyName = dwcMaxSubKeyName + 1;
  57.  
  58.            // Get data about the sub key.
  59.            lStatus = RegEnumKeyEx( hKey, dwSubKeyIndex, lpszSubKeyName,
  60.                                    &dwcSubKeyName, NULL, NULL, NULL, NULL );
  61.            if ( lStatus == ERROR_SUCCESS )
  62.            {
  63.                wsprintf( szBuffer, "%s> %3d: Key=%s", szLevel,
  64.                          dwSubKeyIndex, lpszSubKeyName );
  65.                SendMessage( hWndListBox, LB_ADDSTRING, 0, (LPARAM) szBuffer );
  66.                if ( RegOpenKeyEx( hKey, lpszSubKeyName, 0, KEY_ALL_ACCESS,
  67.                                   &hNewKey ) == ERROR_SUCCESS )
  68.                    EnumerateRegistry( hNewKey, hWndListBox );
  69.                RegCloseKey( hNewKey );
  70.            }
  71.            dwSubKeyIndex++;
  72.        } while ( lStatus==ERROR_SUCCESS );
  73.  
  74.        // housekeeping - remove the data we allocated.
  75.        HeapFree( GetProcessHeap( ), 0, lpszSubKeyName );
  76.        HeapFree( GetProcessHeap( ), 0, lpszValueName );
  77.        HeapFree( GetProcessHeap( ), 0, lpszValueData );
  78.    }
  79.    iLevel--;       // Level is completed.
  80. }
  81.  
  82. LRESULT FAR PASCAL WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  83. {
  84.     switch( uMsg )
  85.     {
  86.          case WM_CREATE:   // Create list box child window to show registry.
  87.             {
  88.                RECT    rectClient;
  89.                LRESULT lRetVal = DefWindowProc(hWnd, uMsg, wParam, lParam);
  90.                GetClientRect( hWnd, &rectClient );       // Center child in parent.
  91.                CreateWindow( "listbox", "child listbox",
  92.                              WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_BORDER,
  93.                              10, 10, rectClient.right - rectClient.left - 20,
  94.                              rectClient.bottom - rectClient.top - 20,
  95.                              hWnd, (HMENU) LISTBOX_ID, hInst, NULL );
  96.                return lRetVal;
  97.             }
  98.         case WM_COMMAND:    // process menu items
  99.             switch( wParam )
  100.             {
  101.                 case IDM_TEST:
  102.                 {
  103.                     HWND hWndListBox = GetDlgItem( hWnd, LISTBOX_ID );
  104.                     if ( hWndListBox )
  105.                     {
  106.                         SendMessage( hWndListBox, LB_RESETCONTENT, 0, 0 );
  107.                         EnumerateRegistry( HKEY_CLASSES_ROOT, hWndListBox );
  108.                     }
  109.                 }
  110.                 break;
  111.                 case IDM_EXIT:
  112.                     DestroyWindow( hWnd );
  113.                     break;
  114.             }
  115.         break;
  116.         case WM_DESTROY:
  117.             PostQuitMessage( 0 )
  118.         break;
  119.         default:            // default windows message processing
  120.             return DefWindowProc( hWnd, uMsg, wParam, lParam );
  121.     }
  122.     return( 0L );
  123. }